/**
 * 通用类型定义
 */

/**
 * 基础数据类型
 */
export type Primitive = string | number | boolean | null | undefined;

/**
 * 可空类型
 */
export type Nullable<T> = T | null;

/**
 * 可选类型
 */
export type Optional<T> = T | undefined;

/**
 * 深度可选类型
 */
export type DeepPartial<T> = {
    [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

/**
 * 深度只读类型
 */
export type DeepReadonly<T> = {
    readonly [P in keyof T]: T[P] extends object ? DeepReadonly<T[P]> : T[P];
};

/**
 * 提取数组元素类型
 */
export type ArrayElement<T> = T extends (infer U)[] ? U : never;

/**
 * 键值对类型
 */
export type KeyValuePair<K = string, V = any> = {
    key: K;
    value: V;
};

/**
 * ID类型
 */
export type ID = string | number;

/**
 * 时间戳类型
 */
export type Timestamp = number;

/**
 * 操作结果类型
 */
export interface Result<T = any, E = Error> {
    success: boolean;
    data?: T;
    error?: E;
    message?: string;
}

/**
 * 分页参数类型
 */
export interface PaginationParams {
    page: number;
    pageSize: number;
    total?: number;
}

/**
 * 分页结果类型
 */
export interface PaginatedResult<T> {
    items: T[];
    pagination: {
        page: number;
        pageSize: number;
        total: number;
        totalPages: number;
        hasNext: boolean;
        hasPrev: boolean;
    };
}

/**
 * 排序参数类型
 */
export interface SortParams {
    field: string;
    order: 'asc' | 'desc';
}

/**
 * 筛选参数类型
 */
export interface FilterParams {
    field: string;
    operator: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'nin' | 'like';
    value: any;
}

/**
 * 查询参数类型
 */
export interface QueryParams {
    filters?: FilterParams[];
    sort?: SortParams[];
    pagination?: PaginationParams;
}

/**
 * 事件类型
 */
export interface Event<T = any> {
    type: string;
    payload: T;
    timestamp: number;
    source?: string;
}

/**
 * 事件监听器类型
 */
export type EventListener<T = any> = (event: Event<T>) => void | Promise<void>;

/**
 * 生命周期钩子类型
 */
export type LifecycleHook = () => void | Promise<void>;

/**
 * 配置接口类型
 */
export interface Config {
    [key: string]: any;
}

/**
 * 验证规则类型
 */
export interface ValidationRule {
    field: string;
    required?: boolean;
    type?: 'string' | 'number' | 'boolean' | 'array' | 'object';
    min?: number;
    max?: number;
    pattern?: RegExp;
    validator?: (value: any) => boolean | string;
}

/**
 * 验证结果类型
 */
export interface ValidationResult {
    isValid: boolean;
    errors: ValidationError[];
}

/**
 * 验证错误类型
 */
export interface ValidationError {
    field: string;
    message: string;
    value?: any;
}

/**
 * 缓存接口类型
 */
export interface Cache<T = any> {
    get(key: string): T | undefined;
    set(key: string, value: T, ttl?: number): void;
    delete(key: string): boolean;
    clear(): void;
    has(key: string): boolean;
    keys(): string[];
    size(): number;
}

/**
 * 日志级别类型
 */
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';

/**
 * 日志条目类型
 */
export interface LogEntry {
    level: LogLevel;
    message: string;
    timestamp: Date;
    context?: any;
    source?: string;
}

/**
 * 性能指标类型
 */
export interface PerformanceMetrics {
    startTime: number;
    endTime?: number;
    duration?: number;
    memoryUsage?: NodeJS.MemoryUsage;
    operation: string;
}

/**
 * 函数重载类型工具
 */
export type Overload<T> = T extends {
    (...args: infer A1): infer R1;
    (...args: infer A2): infer R2;
    (...args: infer A3): infer R3;
    (...args: infer A4): infer R4;
} ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3, (...args: A4) => R4]
    : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
        (...args: infer A3): infer R3;
    } ? [(...args: A1) => R1, (...args: A2) => R2, (...args: A3) => R3]
    : T extends {
        (...args: infer A1): infer R1;
        (...args: infer A2): infer R2;
    } ? [(...args: A1) => R1, (...args: A2) => R2]
    : T extends (...args: infer A) => infer R ? [(...args: A) => R]
    : never;
